
local skynet = require "skynet"
local log = require "Log"
local CommandBase = require ".common.CommandBase"
require "common"

local AssertEx = AssertEx
local Command = class("Command",CommandBase)
---------------------------------------------------------
-- Private
---------------------------------------------------------
function Command:ctor(message,machine)
	self.super:ctor(message,machine)
	self:register()
end

--注册本服务里的消息
function Command:register()
	self.msg:registerSelf('enter_table_req',handler(self,self.enter_room))
	self.msg:registerSelf('leave_table_req',handler(self,self.leave_room))	
	self.msg:registerSelf('closeprivate_req',handler(self,self.close_private)) --关闭私人房
	
	self.msg:registerSelf('pdk_status_req',handler(self,self.get_status))
	self.msg:registerSelf('mj_close_vote_req',handler(self,self.close_vote)) --投票

	self.msg:registerSelf('mj_cancel_req',handler(self,self.cancel))
	self.msg:registerSelf('mj_chi_req',handler(self,self.chi))
	self.msg:registerSelf('mj_peng_req',handler(self,self.peng))
	self.msg:registerSelf('mj_gang_req',handler(self,self.gang))
	self.msg:registerSelf('mj_hu_req',handler(self,self.hu))
	self.msg:registerSelf('mj_play_card_req',handler(self,self.play_card))
	self.msg:registerSelf('mj_ask_hai_di_req',handler(self,self.ask_hai_di))
	self.msg:registerSelf('mj_vote_result_req',handler(self,self.vote_result))

	self.msg:registerSelf('mj_room_info_req',handler(self,self.get_room_info)) --
	self.msg:registerSelf('mj_ready_req',handler(self,self.ready)) --准备

end

----------------------------------------------------------------------------------------
---游戏里的操作
------------------------------------------------------------------------------------------------
function Command:cancel(playerid)
	local seatid = self.machine:can_accept_req(playerid)
	if not seatid then
		return
	end

	if not self.machine:is_game_started(playerid) then
		return
	end
	self.machine:sameone_cancel(seatid)
end

function Command:chi( playerid,args )
	local seatid = self.machine:can_accept_req(playerid)
	if not seatid then
		return
	end
	if not self.machine:is_game_started(playerid) then
		return
	end
	self.machine:sameone_try_chi(seatid,args.cards)
end

function Command:peng( playerid,args )
	local seatid = self.machine:can_accept_req(playerid)
	if not seatid then
		return
	end
	if not self.machine:is_game_started(playerid) then
		return
	end
	self.machine:sameone_try_peng(seatid,args.card)
end

function Command:gang( playerid,args )
	local seatid = self.machine:can_accept_req(playerid)
	if not seatid then
		return
	end

	if not self.machine:is_game_started(playerid) then
		return
	end
	self.machine:sameone_gang(seatid,args.card,args.is_gang)
end

function Command:hu( playerid )
	local seatid = self.machine:can_accept_req(playerid)
	if not seatid then
		return
	end

	if not self.machine:is_game_started(playerid) then
		return
	end
	self.machine:sameone_try_hu(seatid)
end

--可能要发数组 因为刚牌的时候有可能有两张？这种情况服务器自动处理?
function Command:play_card( playerid,args )
	local seatid = self.machine:can_accept_req(playerid)
	if not seatid then
		return
	end

	if not self.machine:is_game_started(playerid) then
		return
	end
	self.machine:sameone_try_play(seatid,args.card)
end

function Command:ask_hai_di( playerid,args )
	
	local seatid = self.machine:can_accept_req(playerid)
	if not seatid then
		return
	end
	if not self.machine:is_game_started(playerid) then
		return
	end
	self.machine:sameone_hai_di(seatid,args.yes)
end
--todo
function Command:vote_result( playerid,args )
	log.debug("Command:vote_result",playerid,args)
	local seatid = self.machine:can_accept_req(playerid)
	if not seatid then
		return
	end

	-- if not self.machine:is_game_started(playerid) then
	-- 	return
	-- end
	self.machine:sameone_vote_result(playerid,args)

end

function Command:get_room_info(playerid,args)
	local player = self.machine:getPlayer(playerid)		
	AssertEx(player,'mj_room_info_res',10)
	if not self.machine:getPlayerSeatId(playerid) then 
		local seatid = self.machine:getPlayerBySeat(1)
		if not seatid then
			seatid = 1
		else
			seatid = nil
		end
		self.machine:sitDown(playerid,seatid)
	end

	local res = self.machine:room_info(playerid)
	return {
		mj_room_info_res = res
	}
end
-- 得到桌子状态
function Command:get_status(playerid,args)
	print("__getStatus_",playerid)
	local player = self.machine:getPlayer(playerid)		
	--无效操作
	AssertEx(player, 'pdk_status_res', 10)	
	if not self.machine:getPlayerSeatId(playerid) then 
		self.machine:sitDown(playerid)
	end
	if args.ready then 
		player.ready = true --准备
	end
	local status = self.machine:getTableStatus(playerid)
	return {
		pdk_status_res = status
	}
end
--准备
function Command:ready(playerid,args)	
		log.debug("Command:ready")
	local res,seatid = self.machine:player_ready(playerid)
	AssertEx(res,'mj_ready_res',16)	
end

--进入桌子
function Command:enter_room(playerid,playerInfo)
	AssertEx(not self.close, 'enter_table_res' , 7) --游戏在维护
	AssertEx(playerid > 0 , 'enter_table_res' , 8) --玩家id错误
	local player = self.machine:getPlayer(playerid)
	if player then		
		if player.disconnect then --断线重连进来						
		else
			--没有断线重复请求进入桌子	
			log.error(">>>>>没有断线重复请求进入桌子________")
		end
	else
		if not self.machine:can_join(playerid) then 
			--人数已满
			AssertEx(false , 'enter_table_res' , 18)
		end
		--第一次进入
		player = table.copy(playerInfo)
		self.machine:add_player(player)	
		local res = self.super.enterTable(self, playerid, playerInfo)	
		local sql = string.format("replace into user_online (uid, sid, game, `leave`) values(%d, %d, %d, %d);", playerid, 11, 11, 2)
		skynet.send("mysql_service", "lua", "execute", sql)	
	end
	player.disconnect = false
	self.machine:nt_onconnect(playerid)
	local result = {
		enter_table_res = {
			tableid = self.tableid,
			gameid = self.gameid,
		}	
	}
	return result
end

--离开
function Command:leave_room(playerid)
	local player = self.machine:getPlayer(playerid)
	if player then												
		if self.machine:leave_table(playerid) then 
			--房主不能离开桌子
			if playerid == self.machine.config.owner then
				return {leave_table_res = GetErrorTable(1401)}
			end
			local res = self.super.leaveTable(self, playerid, playerInfo)	
			local sql = string.format("delete from user_online where uid = %d;", playerid)
			skynet.send("mysql_service", "lua", "execute", sql)
			return {leave_table_res = {tableid = self.tableid}}
		end		
		--游戏已开始，请在休息时再退出！
		return {leave_table_res = GetErrorTable(9)}
	else
		--未进入房间
		return {leave_table_res = GetErrorTable(10)}		
	end
	return false
end


function Command:close_private(playerid)
	--没开局直接解散，开局后要投票
	if self.machine:close_private_room(playerid) then
		return true
	end
	return false
end


function Command:close_vote(playerid,args)
	print("__________close_vote__________",args)
	if self.machine:close_vote(playerid,args) then
		return true
	end
	return false
end

-- 得到桌子状态
function Command:get_status(playerid,args)
	print("__getStatus_",playerid)
	local player = self.machine:getPlayer(playerid)		
	--无效操作
	AssertEx(player, 'pdk_status_res', 10)	
	if not self.machine:getPlayerSeatId(playerid) then 
		self.machine:sitDown(playerid)
	end
	if args.ready then 
		player.ready = true --准备
	end
	local status = self.machine:getTableStatus(playerid)
	return {
		pdk_status_res = status
	}
end

--记牌
function Command:jipai(playerid,args)	
	local res = self.machine:getOutCards(playerid)
	AssertEx(res,'pdk_jipai_res',16)	
	return {
		pdk_jipai_res = {
			-- rest_card = res,
			out_card = res,
		}
	}		
end

return Command